<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="150px">
          <el-form-item label="群类型" prop="groupType">
            <el-input v-model="form.groupType" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="群名称" prop="groupName">
            <el-input v-model="form.groupName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="群封面图" prop="groupImg">
              <MaterialList
                      v-model="groupImgArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="群二维码" prop="wechatImg">
              <MaterialList
                      v-model="wechatImgArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="管理员1">
            <el-input v-model="form.wechatMemberId1" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="微信号1">
            <el-input v-model="form.wechatCode1" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="管理员2">
            <el-input v-model="form.wechatMemberId2" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="微信号2">
            <el-input v-model="form.wechatCode2" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="管理员3">
            <el-input v-model="form.wechatMemberId3" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="微信号3">
            <el-input v-model="form.wechatCode3" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="省市区" prop="areaId">
            <el-input
              v-if="false"
              v-model="form.areaId"
              style="width: 370px;"
            />
            <div v-if="form.areaId">
              {{ dialogTableDataAreaId.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataAreaId"
              @select-callback="dialogTableSelectedCallBackAreaId"
            ></dialogTable>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('groupType')" prop="groupType" label="群类型" />
        <el-table-column v-if="columns.visible('groupName')" prop="groupName" label="群名称" />
          <el-table-column v-if="columns.visible('groupImg')" prop="groupImg" label="群封面图">
            <template slot-scope="scope">
              <a :href="scope.row.groupImg" style="color: #42b983" target="_blank">
                <img :src="scope.row.groupImg" alt="点击打开" class="table-img" />
              </a>
            </template>
          </el-table-column>
          <el-table-column v-if="columns.visible('wechatImg')" prop="wechatImg" label="群二维码">
            <template slot-scope="scope">
              <a :href="scope.row.wechatImg" style="color: #42b983" target="_blank">
                <img :src="scope.row.wechatImg" alt="点击打开" class="table-img" />
              </a>
            </template>
          </el-table-column>
        <el-table-column v-if="columns.visible('wechatMemberId1')" prop="wechatMemberId1" label="管理员1" />
        <el-table-column v-if="columns.visible('wechatCode1')" prop="wechatCode1" label="微信1" />
        <el-table-column v-if="columns.visible('wechatMemberId2')" prop="wechatMemberId2" label="管理员2" />
        <el-table-column v-if="columns.visible('wechatCode2')" prop="wechatCode2" label="微信2" />
        <el-table-column v-if="columns.visible('wechatMemberId3')" prop="wechatMemberId3" label="管理员3" />
        <el-table-column v-if="columns.visible('wechatCode3')" prop="wechatCode3" label="微信3" />

        <el-table-column v-if="columns.visible('areaName')" prop="areaName" label="区域" />
        <el-table-column v-permission="['admin','tanzerActivityWechatGroup:edit','tanzerActivityWechatGroup:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTanzerActivityWechatGroup from '@/api/tanzer/tanzerActivityWechatGroup'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import dialogTable from "@/views/components/dialogTable/index.vue";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '微信群管理', url: 'api/tanzerActivityWechatGroup', sort: 'id,desc', crudMethod: { ...crudTanzerActivityWechatGroup }})
const defaultForm = { id: null, groupName: null, groupNum: null, groupImg: null, wechatImg: null, areaId: null, wechatCode1: null, wechatCode2: null, wechatCode3: null, wechatMemberId1: null, wechatMemberId2: null, wechatMemberId3: null, isDel: null, createTime: null, createBy: null, updateBy: null, updateTime: null }
export default {
  name: 'TanzerActivityWechatGroup',
  components: {dialogTable, pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      groupImgArr:[],wechatImgArr:[],
      permission: {
        add: ['admin', 'tanzerActivityWechatGroup:add'],
        edit: ['admin', 'tanzerActivityWechatGroup:edit'],
        del: ['admin', 'tanzerActivityWechatGroup:del']
      },
      rules: {
        groupName: [
          { required: true, message: '群名称不能为空', trigger: 'blur' }
        ],
        groupImg: [
          { required: true, message: '群封面图不能为空', trigger: 'blur' }
        ],
        wechatImg: [
          { required: true, message: '群二维码不能为空', trigger: 'blur' }
        ],
        areaId: [
          { required: true, message: '区域ID不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'groupName', display_name: '群名称' },
        { key: 'wechatCode1', display_name: '管理员微信号1' },
        { key: 'wechatCode2', display_name: '管理员微信号2' },
        { key: 'wechatCode3', display_name: '管理员微信号3' },
        { key: 'wechatMemberId1', display_name: '管理员1' },
        { key: 'wechatMemberId2', display_name: '管理员2' },
        { key: 'wechatMemberId3', display_name: '管理员3' }
      ],
      //自定义lov数据源
      dialogTableDataAreaId: {
        title: "省市区",
        dialogVisible: false,
        url: "api/tanzerBaseRegion",
        sort: "id,desc",
        buttonContext: "请选择省市区",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'provinceName', display_name: '省级名称' },
          { key: 'cityName', display_name: '市级名称' },
          { key: 'districtName', display_name: '区级名称' },
        ],
        elTtableColumns: [
          {
            columnProp: "provinceName",
            columnLabel: "省级名称"
          },
          {
            columnProp: "cityName",
            columnLabel: "市级名称"
          },
          {
            columnProp: "districtName",
            columnLabel: "区级名称"
          }
        ]
      },
    }
  },
  watch: {
    "groupImgArr":function(val) {
      if (val) {
        this.form.groupImg = val.join(",");
      }
    },
    "wechatImgArr":function(val) {
      if (val) {
        this.form.wechatImg = val.join(",");
      }
    },
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.groupName
        delete this.crud.params.wechatCode1
        delete this.crud.params.wechatCode2
        delete this.crud.params.wechatCode3
        delete this.crud.params.wechatMemberId1
        delete this.crud.params.wechatMemberId2
        delete this.crud.params.wechatMemberId3
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.groupImg && form.id) {
        this.groupImgArr = form.groupImg.split(",");
      }else{
        this.groupImgArr=[]
      }
      if (form.wechatImg && form.id) {
        this.wechatImgArr = form.wechatImg.split(",");
      }else{
        this.wechatImgArr=[]
      }
    },

    [CRUD.HOOK.afterToEdit](crud, form) {

      this.setInputValue(
        this.dialogTableDataAreaId,
        "showInputValue",
        form,
        "areaId",
        "content",
        0,
        "areaName",
        "api/tanzerBaseRegion",
        {
          page: 0,
          size: 10,
          sort: "id,desc",
          id: form.areaId
        }
      );
    },

    //省市区数据源
    dialogTableSelectedCallBackAreaId(row) {
      this.form.areaId = row.id;
      if (this.form.areaId) {
        this.dialogTableDataAreaId.showInputValue = row.areaName;
      }
    },

  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
